﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Web;
using ELGGAPI.Models;
using MySql.Data.MySqlClient;
namespace ELGGAPI.Data
{
    
    public class PlayerInstanceDataModels
    {
        public int? playerid { get; set; }
        public int? gameid { get; set; }
        public int? skilllevel { get; set; }
        public int? experiencelevel { get; set; }
        public int? gamescore { get; set; }
        //public int? highestskilllevel { get; set; }
        public long? recorddate { get; set; }
    }
    public class PlayerInstanceGraphModels
    {
        public double? skilllevel { get; set; }
        public double? score { get; set; }
        public IEnumerable<PlayerInstanceGraphDataModels> graphdata { get; set; }
    }
    public class PlayerInstanceGraphDataModels
    {

        public int? date { get; set; }
        public int? skilllevel { get; set; }
        public int? gamescore { get; set; }
        //public int? gameid { get; set; }
        //public int? playerid { get; set; }

    }
    public class PlayerInstanceGraphDomain
    {
        public static PlayerInstanceGraphModels GetPlayerInstanceGraph(int pPlayerId, int pGameID, int? pMonth, int? pYear)
        {
            List<PlayerInstanceGraphDataModels> list = new List<PlayerInstanceGraphDataModels>();
            PlayerInstanceGraphModels Graph = new PlayerInstanceGraphModels();

            DataSet dataSet = new DataSet();
            using (MySqlConnection connection = new MySqlConnection(APIHelper.ConnectionString))
            {
                using (MySqlCommand selectCommand = new MySqlCommand(APIHelper.DBName + "my_getPerformanceofPlaygraph", connection))
                {
                    if (pMonth != 0)
                        selectCommand.Parameters.AddWithValue("pMonth", pMonth);
                    else
                        selectCommand.Parameters.AddWithValue("pMonth", DBNull.Value);

                    if (pYear == 0)
                        selectCommand.Parameters.AddWithValue("pYear", DBNull.Value);
                    else
                        selectCommand.Parameters.AddWithValue("pYear", pYear);

                    if (pGameID == 0)
                        selectCommand.Parameters.AddWithValue("pGameID", DBNull.Value);
                    else
                        selectCommand.Parameters.AddWithValue("pGameID", pGameID);

                    //if (pProviderID == 0)
                    //    selectCommand.Parameters.AddWithValue("pProviderID", DBNull.Value);
                    //else
                    //    selectCommand.Parameters.AddWithValue("pProviderID", pProviderID);

                    if (pPlayerId == 0)
                        selectCommand.Parameters.AddWithValue("pGameUserID", DBNull.Value);
                    else
                        selectCommand.Parameters.AddWithValue("pGameUserID", pPlayerId);


                    selectCommand.CommandType = CommandType.StoredProcedure;
                    using (MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter(selectCommand))
                        ((DataAdapter)mySqlDataAdapter).Fill(dataSet);
                }
            }
            if (dataSet.Tables.Count > 0)
            {
                if (dataSet.Tables[0].Rows.Count > 0)
                {
                    DataRow drsum = dataSet.Tables[0].Rows[0];
                    Graph.skilllevel = double.Parse("0" + drsum["HighestScore"].ToString());
                    Graph.score = double.Parse("0" + drsum["HighestSkillLevel"].ToString());
                }
                foreach (DataRow dataRow in (InternalDataCollectionBase)dataSet.Tables[1].Rows)
                    list.Add(new PlayerInstanceGraphDataModels()
                    {

                        date = int.Parse("0" + dataRow["DateRecordAdded"].ToString()),
                        skilllevel = int.Parse("0" + dataRow["SkillLevel"].ToString()),
                        gamescore = int.Parse("0" + dataRow["Gamescore"].ToString())
                        //gameid = int.Parse("0" + dataRow["gameid"].ToString()),
                        //playerid = int.Parse("0" + dataRow["GamePlayerId"].ToString())
                    });
                Graph.graphdata = list;
            }
            return Graph;
        }
    }
    public class PlayerInstanceDataDomainModel
    {

        public static string AddUpdatePlayerInstanceData(PlayerInstanceDataModels playerinstancedata)
        {
            string isInstanceDataGUID = "";


            using (MySqlConnection connection = new MySqlConnection(APIHelper.ConnectionString))
            {
                using (MySqlCommand mySqlCommand = new MySqlCommand(APIHelper.DBName + "my_gamePlayerPerformanceDataInsertUpdate", connection))
                {
                    mySqlCommand.Parameters.AddWithValue("pGamePlayerID", playerinstancedata.playerid);
                    mySqlCommand.Parameters.AddWithValue("pGameID", playerinstancedata.gameid);
                    mySqlCommand.Parameters.AddWithValue("pSkillLevel", playerinstancedata.skilllevel);
                    mySqlCommand.Parameters.AddWithValue("pExperienceLevel", playerinstancedata.experiencelevel);
                    mySqlCommand.Parameters.AddWithValue("pGameScore", playerinstancedata.gamescore);
                    //mySqlCommand.Parameters.AddWithValue("pHighestSkillLevel", playerinstancedata.highestskilllevel);
                    mySqlCommand.Parameters.AddWithValue("pDateRecordAdded", playerinstancedata.recorddate);

                    mySqlCommand.CommandType = CommandType.StoredProcedure;
                    mySqlCommand.Connection.Open();
                    MySqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
                    while (mySqlDataReader.Read())
                    {

                        isInstanceDataGUID = mySqlDataReader["vPlayerPerformanceDataID"].ToString();

                    }
                    mySqlDataReader.Close();
                    connection.Close();
                }
            }
            return isInstanceDataGUID;
        }
        public static List<PlayerInstanceDataModels> GetPlayerInstanceDataList(int pProviderID, int pPlayerId, int pGameID)
        {
            List<PlayerInstanceDataModels> list = new List<PlayerInstanceDataModels>();
            DataSet dataSet = new DataSet();
            using (MySqlConnection connection = new MySqlConnection(APIHelper.ConnectionString))
            {
                using (MySqlCommand selectCommand = new MySqlCommand(APIHelper.DBName + "my_gamePerformancelist", connection))
                {

                    if (pProviderID == 0)
                        selectCommand.Parameters.AddWithValue("pProviderID", DBNull.Value);
                    else
                        selectCommand.Parameters.AddWithValue("pProviderID", pProviderID);

                    if (pPlayerId == 0)
                        selectCommand.Parameters.AddWithValue("pGameUserID", DBNull.Value);
                    else
                        selectCommand.Parameters.AddWithValue("pGameUserID", pPlayerId);

                    if (pGameID == 0)
                        selectCommand.Parameters.AddWithValue("pGameID", DBNull.Value);
                    else
                        selectCommand.Parameters.AddWithValue("pGameID", pGameID);


                    selectCommand.CommandType = CommandType.StoredProcedure;
                    using (MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter(selectCommand))
                        ((DataAdapter)mySqlDataAdapter).Fill(dataSet);
                }
            }
            if (dataSet.Tables.Count > 0)
            {
                foreach (DataRow dataRow in (InternalDataCollectionBase)dataSet.Tables[0].Rows)
                    list.Add(new PlayerInstanceDataModels()
                    {
                        gameid = int.Parse("0" + dataRow["GameId"].ToString()),
                        playerid = int.Parse("0" + dataRow["GamePlayerID"].ToString()),
                        skilllevel = int.Parse("0" + dataRow["SkillLevel"].ToString()),
                        experiencelevel = int.Parse("0" + dataRow["ExperienceLevel"].ToString()),
                        gamescore = int.Parse("0" + dataRow["GameScore"].ToString()),
                        //highestskilllevel = int.Parse("0" + dataRow["HighestSkillLevel"].ToString()),
                        recorddate = int.Parse("0" + dataRow["DateRecordAdded"].ToString())
                        
                    });
            }
            return list;
        }
    }
}